package com.zggk.iroad.api;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

public class MyAppliCation extends Application {
    public static MyAppliCation self;// 应用实例
    public static SPUtils spUtils;
    private List<Activity> activities;
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);
        spUtils = SPUtils.getInstance("zggk_demo_iroad");
        self = this;
        initOkGo();
        mContext = getApplicationContext();

    }

    private void initOkGo() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        OkGo.getInstance().init(this)                       //必须调用初始化
                .setOkHttpClient(builder.build())               //建议设置OkHttpClient，不设置将使用默认的
                .setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)   //全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(3);                            //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0
    }
    /**
     * 将新的Activity入栈存储
     *
     * @param activity Activity实例
     */
    public void addActivity(Activity activity) {
        if (activity != null) {
            if (this.activities == null) {
                activities = new ArrayList<>();
            }
            activities.add(activity);
        }
    }

    /**
     * 移除已经存在的Activity
     *
     * @param activity Activity实例
     */
    public void removeActivity(Activity activity) {
        if (activity != null && this.activities != null) {
            this.activities.remove(activity);
        }
    }
    /**
     * 移除所有已经存在的Activity
     */
    private void removeAllActivities() {
        if (this.activities != null) {
            for (int i = this.activities.size() - 1; i >= 0; i--) {
                this.activities.remove(i);
            }
        }
    }
    /**
     * 退出应用
     */
    public void exitApp() {
        removeAllActivities();
        System.exit(0);
    }
    public static Context getContext() {
        return mContext;
    }
}

